import React from 'react'
import {Table} from 'antd'
import Utils from '../../utils/utils'


export default class JustTable extends React.Component {

    onRowClick = (record, index) => {
        console.log(record, index)
        let pk = this.props.pk || 'id'
        if (this.props.rowSelection == 'checkbox') {
            let selectedRowKeys = this.props.selectedRowKeys
            let selectedItem = this.props.selectedItem
            let selectedIds = this.props.selectedIds
            if (selectedIds) {
                const i = selectedIds.indexOf(record[pk])
                if (i == -1) {//选中新的
                    selectedIds.push(record[pk])
                    selectedRowKeys.push(index)
                    selectedItem.push(record)
                } else {////选中旧的
                    selectedIds.splice(i, 1)
                    selectedRowKeys.splice(i, 1)
                    selectedItem.splice(i, 1)
                }
            } else {
                selectedIds = [record[pk]]
                selectedRowKeys = [index]
                selectedItem = [record]
            }
            this.props.updateSelectedItem(selectedRowKeys, selectedItem, selectedIds)
        } else {
            let selectedRowKeys = [index]
            let selectedItem = record
            this.props.updateSelectedItem(selectedRowKeys, selectedItem)
        }
    }
    tableInit = () => {
        let row_selection = this.props.rowSelection
        let selectedRowKeys = this.props.selectedRowKeys
        const rowSelection = {
            type: 'radio',
            selectedRowKeys,
            // onChange: (selectedRowKeys, selectedItem)=>{
            //     console.log('选中的onChange')
            //     console.log(selectedItem)
            //     // this.setState({
            //     //     selectedRowKeys:selectedRowKeys,
            //     //     selectedItem:selectedItem,
            //     // })
            // },
            onSelect: (selectedItem) => { //点击事件
                let pk = this.props.pk || 'id'
                let selectIndex = null
                this.props.dataSource.filter((item, index) => {
                    if (item[pk] == selectedItem[pk]) {
                        return selectIndex = index
                    }
                })

                this.onRowClick(selectedItem, selectIndex)
            },
            onSelectAll: (selected) => { //全选处理
                let selectedIds = []
                let selectedRowKeys = []
                let selectedItem = []
                let pk = this.props.pk || 'id'
                if (selected) {
                    this.props.dataSource.filter((item, index) => {
                        selectedIds.push(item[pk])
                        selectedRowKeys.push(index)
                        selectedItem.push(item)
                    })

                }
                this.props.updateSelectedItem(selectedRowKeys, selectedItem, selectedIds)
            },
        }
        if (row_selection == 'checkbox') {
            rowSelection.type = 'checkbox'
        } else if (row_selection == 'radio') {
            row_selection = 'radio'
        } else {
            row_selection = false
        }


        return <Table
            bordered
            {...this.props}
            // dataSource={this.state.dataSource2}
            // columns={columns2}
            // pagination={false}
            rowSelection={row_selection ? rowSelection : null}

            onRow={(record, index) => {
                return {
                    onClick: () => {
                        if (!row_selection) {
                            return
                        }
                        this.onRowClick(record, index)
                    },
                };
            }}
        />
    }

    render() {
        return (
            <div>
                {this.tableInit()}
            </div>
        )
    }
}



